home *** CD-ROM | disk | FTP | other *** search
- Path: news.infi.net!usenet
- From: nngis@norfolk.infi.net (Greg DiGiorgio)
- Newsgroups: comp.lang.c
- Subject: Re: strange problem with strcpy()
- Date: 1 Jan 1996 02:49:45 GMT
- Organization: Customer of InfiNet
- Message-ID: <4c7i49$q7b@news.infi.net>
- References: <DKFsII.FyJ@iglou.com>
- NNTP-Posting-Host: h-talisman.norfolk.infi.net
- Mime-Version: 1.0
- X-Newsreader: WinVN 0.99.3
-
- In article <DKFsII.FyJ@iglou.com>, quare@iglou2.iglou.com says...
- >
- >Some background. tc++ 3.0 for dos. Basically I have this routine that
- >reads in files from the current directory and makes an malloc'ed array of
- >structures which hold info about those files (attribs, filename/ext, size,
- >etc.). More detailed info on request.
- >
- >Anyway, this routine uses a pointer to write to this 'array'. (as in:
- >pointer->name, pointer->size, etc.). strcpy(pointer->name,
- >fileinfo->name) (fileinfo is the structure written to by the dos service)
- >*sometimes* works, yet sometimes it will place a \0 in the first position
- >of pointer->name. for example..
- >
- >if fileinfo->name == "test.c", strcpy will place "\0est.c" into
- >pointer->name. Any clues as to how this is happening? And no,
- >pointer->name was not "test.c" before the call to strcpy(). Please... I
- >beg of you... :(
- >
- >--
- >!quare
- >
-
- STRCPY is not the problem. I have worked with every version of turbo C for
- DOS since V2.0 in 1988 and believe me - if you are having a problem with
- STRCPY, its your code not the string library.
-
- Now, what could cause your problem? As you know STRCPY is a dumb function
- that copies bytes from whatever source address you give, up utnil it hits a
- NULL to whatever destination address you give. Since you say you are using
- MALLOC and/or REALLOC, my guess is that you are screwing up your pointer
- arithmetic. It happens to everyone - experienced and newbie alike!
-
- Let's assume that you have 2 vars, s1 and s2. s1 = "ABCD" and s2 =
- (char)NULL. So if you "strcpy(s1,s2)" you'll get "\0BCD" in s1. If your
- malloc fails, it returns a NULL which is easy enough to check.
-
- I know this won't answer your problem. My guess is that you need another
- "set of eyes" to spot your bug. Right now, though, I don't volunteer myself,
- but suggest that you put your code up in the newsgrp. Maybe not all of it -
- perhaps just the part where you know it goes bad.
-
- Good luck,
- Greg DiGiorgio
-
-